﻿//----------------------------------------------------
//    版权：
//    作者：
//建立时间：2012-09-11 00:00:33
//    描述：
//----------------------------------------------------
using System;
using System.Data;
using System.Text;


using WinnerCMS.Common;
using System.Linq;
using System.Collections.Generic;
using WinnerCMS.Model;
using System.Data.Entity;
namespace WinnerCMS.Data
{
    /// <summary>
    /// 数据访问层CMS_Favorite。
    /// </summary>
    public class FavoriteDal
    {
        #region  添加数据

        /// <summary>
        /// 添加数据
        /// </summary>
        public void Add(Favorite model)
        {
            using (Entities db = new Entities())
            {
                db.Favorites.Add(model);
                db.SaveChangesAsync().Wait();
            }
        }
        #endregion

        #region  修改数据
        /// <summary>
        /// 修改
        /// </summary>
        public void Edit(Favorite model)
        {
            using (Entities db = new Entities())
            {
                db.Entry(model).State = EntityState.Modified;
                db.SaveChangesAsync().Wait();
            }
        }
        #endregion

        #region  删除数据
        /// <summary>
        /// 删除数据
        /// </summary>
        /// <param name="Id"></param>
        public void Delete(int Id)
        {
            using (Entities db = new Entities())
            {
                var q = db.IPBlackList.Where(x => Id == x.Id).First();


                db.IPBlackList.Remove(q);


                db.SaveChangesAsync().Wait();
            }

        }

        #endregion

        #region  绑定分页
        /// <summary>
        /// 绑定分页
        /// </summary>
        /// <param name="UserID"></param>
        /// <param name="pp"></param>
        /// <returns></returns>
        public IList<Favorite> BindList(int UserID, PagingParam pp)
        {
            using (Entities db = new Entities())
            {

                var query = from x in db.Favorites
                            where x.UserId == UserID
                            select x;

                pp.RecordCount = query.Count();
                return query.OrderByDescending(x => x.Updatetime).Skip(pp.Skip).Take(pp.PageSize).ToList();

            }
        }
        #endregion
    }
}